﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;


namespace Golf.WebUI.AdminShow.CommoditiesMmanagement
{
    public partial class Golf_BrandTypeZSG : Golf.WebUI.App_code.ValidateLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Object obj = CookiesHelper.GetCookie("_AdminID");
            txtAddAdminId.Text = obj.ToString();
            if (!Page.IsPostBack)
            {
                switch (Request.Params["action"])
                {
                    case "modify"://修改
                        ShowInfo();
                        break;
                    case "add":
                        _Add();
                        break;
                    case "del":
                        _Del();
                        break;
                }

            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        private void _Del()
        {
            Golf.BLL.Golf_BrandType bll = new Golf.BLL.Golf_BrandType();
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int Id = (Convert.ToInt32(Request.Params["id"]));
                if (bll.Delete(Id))
                {
                    Response.Write("成功删除,");
                }
                else
                {
                    Response.Write("删除失败,");
                }
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        private void _Add()
        {
            _action.Value = "add";
        }
        /// <summary>
        /// 修改
        /// </summary>
        private void ShowInfo()
        {
            _action.Value = "modify";
            int Id = 0;
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                Id = (Convert.ToInt32(Request.Params["id"]));
            }
            Golf.BLL.Golf_BrandType bll = new Golf.BLL.Golf_BrandType();
            Golf.Model.Golf_BrandType model = bll.GetModel(Id);
            this.lblId.Text = model.Id.ToString();
            this.txtTypeName.Text = model.TypeName;
            this.txtAddAdminId.Text = model.AddAdminId.ToString();
            this.txtAddTime.Text = model.AddTime.ToString();
            this.chkIsCommend.Checked = model.IsCommend;
            this.chkIsDisplay.Checked = model.IsDisplay;

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtTypeName.Text.Trim().Length == 0)
            {
                strErr += "品牌分类名称不能为空！\\n";
            }
            if (!PageValidate.IsNumber(txtAddAdminId.Text))
            {
                strErr += "添加管理人id	Golf_Ad格式错误！\\n";
            }
            if (!PageValidate.IsDateTime(txtAddTime.Text))
            {
                strErr += "添加品牌分类时间格式错误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string TypeName = this.txtTypeName.Text;
            int AddAdminId = int.Parse(this.txtAddAdminId.Text);
            DateTime AddTime = DateTime.Parse(this.txtAddTime.Text);
            bool IsCommend = this.chkIsCommend.Checked;
            bool IsDisplay = this.chkIsDisplay.Checked;

            Golf.Model.Golf_BrandType model = new Golf.Model.Golf_BrandType();
            model.TypeName = TypeName;
            model.AddAdminId = AddAdminId;
            model.AddTime = AddTime;
            model.IsCommend = IsCommend;
            model.IsDisplay = IsDisplay;
            Golf.BLL.Golf_BrandType bll = new Golf.BLL.Golf_BrandType();

            if (_action.Value == "add")
            {
                if (bll.Add(model) > 0)
                {
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('添加成功！');", true);
                }
                else
                {

                    Page.ClientScript.RegisterStartupScript(typeof(Page), "", "ymPrompt.errorInfo('保存失败');", true);
                }
            }
            if (_action.Value == "modify")
            {
                int Id = int.Parse(this.lblId.Text);
                model.Id = Id;

                if (bll.Update(model))
                {
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('修改成功！');", true);
                }
                else
                {

                    Page.ClientScript.RegisterStartupScript(typeof(Page), "", "ymPrompt.errorInfo('修改失败');", true);
                }
            }


        }
    }
}
